package com.devin.controller;

import com.devin.pojo.Permission;
import com.devin.pojo.Role;
import com.devin.service.PermissionService;
import com.devin.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("roles")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private PermissionService permissionService;

    @RequestMapping("/getRoles")
//    @Secured(value = {"ROLE_ADMIN","ROLE_USER"})
    public ModelAndView getRoles(@RequestParam(value = "page",defaultValue = "1") Integer page,@RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        List<Role> roles = roleService.getRolesAll(page,pageSize);
        PageInfo<Role> rolePageInfo = new PageInfo<>(roles);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("rolePageInfo",rolePageInfo);
        modelAndView.setViewName("pages/role-pages-list");
        return modelAndView;
    }

    @GetMapping("/addRoleWithPermission")
    public ModelAndView addRoleWithPermission(){
        List<Permission> permissions = permissionService.getPermissions();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("permissions",permissions);
        modelAndView.setViewName("pages/role-add");
        return modelAndView;
    }

    @PostMapping("/addRole")
    public String addRole(Role role,Integer[] permissionIds){
        roleService.addRole(role,permissionIds);
        return "redirect:/roles/getRoles";
    }

    @RequestMapping("/delRole")
    public String delRole(Integer roleId){
        roleService.delRole(roleId);
        return "redirect:/roles/getRoles";
    }

    @RequestMapping("/getRoleById")
    public ModelAndView getRoleById(Integer roleId){
        Role role = roleService.getRoleById(roleId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("role",role);
        modelAndView.setViewName("pages/roles-detail");
        return  modelAndView;
    }

    @RequestMapping("/addPermissions")
    public String addPermissions(Integer roleId,Integer[] permissionIds){
        roleService.addPermissions(roleId,permissionIds);
        return "redirect:/roles/getRoleById?roleId="+roleId;
    }

    @RequestMapping("/delPermissionByRoleId")
    public String delPermissionByRoleId(Integer roleId,Integer permissionId){
        roleService.delPermissionByRoleId(roleId,permissionId);
        return "redirect:/roles/getRoleById?roleId="+roleId;
    }

    @PostMapping("/delRolesChecked")
    @ResponseBody
    public String delRolesChecked(Integer[] ids){
        roleService.delRolesChecked(ids);
        return "done";
    }

}
